//
//  QYMAlipayManager.m
//  QYMShareSDK
//
//  Created by Jack Luo on 2019/6/24.
//

#import "QYMAlipayManager.h"
#import "AlipaySDK/AlipaySDK.h"

@implementation QYMAlipayManager

#pragma mark - 是否安装支付宝
+ (BOOL)isAlipayAppInstalled{
    
    NSString *urlStr = @"alipay:";
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlStr]]) {
        return YES;
    }
    
    return NO;
}

+ (void)loginWithAuthInfoStr:(NSString *)authInfoStr
                   appScheme:(NSString *)appScheme
                       block:(void(^)(BOOL success,NSString *code,NSString *errorMsg))resultBlock{
    
    [[AlipaySDK defaultService] auth_V2WithInfo:authInfoStr
                                     fromScheme:appScheme
                                       callback:^(NSDictionary *resultDic) {
                                           NSLog(@"result = %@",resultDic);
                                           // 解析 auth code
                                           NSString *result = resultDic[@"result"];
                                           NSString *authCode = nil;
                                           if (result.length>0) {
                                               NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                                               for (NSString *subResult in resultArr) {
                                                   if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                                                       authCode = [subResult substringFromIndex:10];
                                                       break;
                                                   }
                                               }
                                           }
                                           NSLog(@"授权结果 authCode = %@", authCode?:@"");
                                           
                                           if (authCode.length > 0) {
                                               if (resultBlock) {
                                                   resultBlock(YES,authCode,nil);
                                               }
                                           }
                                           else{
                                               
                                               NSString *memo         = resultDic[@"memo"];
                                               NSString *resultStatus = resultDic[@"resultStatus"];
                                               
                                               NSMutableString *errMsg = [NSMutableString string];
                                               //6001 用户取消不显示错误信息
                                               if (![resultStatus isEqualToString:@"6001"]) {
                                                   
                                                   if (memo.length > 0) {
                                                       [errMsg appendFormat:@"支付宝授权失败,%@",memo];
                                                   }
                                                   else{
                                                       [errMsg appendFormat:@"支付宝授权失败"];
                                                   }
                                                   if (resultStatus.length > 0) {
                                                       [errMsg appendFormat:@"，状态码:%@",resultStatus];
                                                   }
                                               }
                                               
                                               if (resultBlock) {
                                                   resultBlock(NO,nil,errMsg);
                                               }
                                           }
                                       }];
}

#pragma mark - 支付
+ (void)payWithOrderStr:(NSString *)orderStr
              appScheme:(NSString *)appScheme
            resultBlock:(void(^)(NSDictionary *dict))reslutBlock{
    
    [[AlipaySDK defaultService] payOrder:orderStr fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"AliPay reslut = %@",resultDic);
        
        if (reslutBlock){
            reslutBlock(resultDic);
        }
    }];
}

@end
